<?php
/**
 * Created by PhpStorm.
 * User: thanatos
 * Date: 2017/11/28
 * Time: 下午11:26
 */

namespace frontend\models;


use common\models\Check;
use common\traits\Model;

class CheckForm extends Check{
    use Model;
    
    public function rules(){
        return [
            [['user_id', 'bl_img','type'], 'required'],
            ['user_id', 'integer'],
            [['bl_img'], 'string', 'max' => 255],
            ['user_id', 'validateUser'],
        ];
    }
    
    public function validateUser($attribute)
    {
        if( !$this->hasErrors() ){
            // 验证是否重复申请
            $model = static::findOne([$attribute => $this->$attribute, 'type' => $this->type]);
            if( $model && ($model->check_time + 3600*24*7) >= time() ){
                $this->addError($attribute, '请勿重新申请');
            }
        }
    }
    
    public function submit(){
        if( !$this->validate() ){
            return false;
        }
        
        $model = new Check($this->attributes);
        $model->save();
        return $model->save() ? $model : false;
    }
    
}